//
//  LFShopViewController.m
//  LoveFruit
//
//  Created by 马海江 on 16/9/26.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFShopViewController.h"
#import "LFShopListViewController.h"
#import "LFShopTableViewCell.h"
static NSString *cellid = @"cell";
@interface LFShopViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign)BOOL diZhi1;
@property (nonatomic, assign)BOOL diZhi2;
@property (nonatomic, assign)BOOL diZhi3;

@property (nonatomic, assign)int count;

@property (nonatomic, weak)UITableView *tableview;
@end

@implementation LFShopViewController

//- (void)viewWillAppear:(BOOL)animated{
//    self.diZhi1 = NO;
//    self.diZhi2 = NO;
//    self.diZhi3 = NO;
//    self.count = 0;
//    [super viewWillAppear:animated];
//    // 地址1通知观察者
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DiZhi1:) name:@"NotificationDIZhi1" object:nil];
//    // 地址2通知观察者
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DiZhi2:) name:@"NotificationDIZhi2" object:nil];
//    // 地址3通知观察者
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DiZhi3:) name:@"NotificationDIZhi3" object:nil];
//}
//- (void) DiZhi1: (NSNotification*) aNotification{
//    self.diZhi1 = YES;
//    self.count += 1;
//}
//- (void) DiZhi2: (NSNotification*) aNotification{
//    self.diZhi2 = YES;
//    self.count += 1;
//}
//- (void) DiZhi3: (NSNotification*) aNotification{
//    self.diZhi3 = YES;
//    self.count += 1;
//}
//-(void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

- (void)viewDidAppear:(BOOL)animated{
//    NSString *tempPath = NSTemporaryDirectory();
//    NSString *filePath1 = [tempPath stringByAppendingPathComponent:@"DiZhi1.data"];
//    LFShopCollectModel *model1 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath1];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        self.count = 0;
    });
    [self.tableview reloadData];
}

- (void)viewDidDisappear:(BOOL)animated{
    self.diZhi1 = YES;
    self.diZhi2 = YES;
    self.diZhi3 = YES;
    self.count = 3;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"店铺收藏";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBuutonItemWithTarget:self action:@selector(backItem) icon:@"btn_backItem" highlighticon:nil];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonWithTitle:@"编辑" image:nil style:UIBarButtonItemStylePlain target:self action:@selector(editClick)];
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithHex:0xefefef];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerNib:[UINib nibWithNibName:@"LFShopTableViewCell" bundle:nil] forCellReuseIdentifier:cellid];
    // 取消系统分割线
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableview = tableView;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.offset(64);
    }];
    
    // 白色的View
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    
    // Button
    UIButton *new = [[UIButton alloc] init];
    new.backgroundColor = [UIColor colorWithHex:0xffd600];
    [new setTitle:@"+ 新增店铺" forState:UIControlStateNormal];
    new.titleLabel.font = [UIFont systemFontOfSize:13];
    [new setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [new.layer setCornerRadius:4];
    [new addTarget:self action:@selector(newButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:new];
    [new mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(bottomView);
        make.width.equalTo(@246);
        make.height.equalTo(@29);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.diZhi1 == YES && indexPath.row == 0) {
        LFShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        cell.name.text = @"爱鲜蜂龙乡店（吉利发超市）";
        cell.time.text = @"营业时间：9：00--22：00";
        cell.address.text = @"昌平区龙乡东区10号楼对面";
        return cell;
    }
    if (self.diZhi2 == YES && indexPath.row == 1) {
        LFShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        cell.name.text = @"爱鲜蜂上奥世纪店（世纪华联）";
        cell.time.text = @"营业时间：9：00--23：00";
        cell.address.text = @"昌平区回龙观镇建材城西路87号院3号楼1层101号";
        return cell;
    }
    if (self.diZhi3 == YES && indexPath.row == 2) {
        LFShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        cell.name.text = @"爱鲜蜂水利宿舍店（雯浩晟源网络超市）";
        cell.time.text = @"营业时间：9：00--22：00";
        cell.address.text = @"北京市海淀区安宁庄后街16号水利宿舍底商";
        return cell;
    }
    else return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}


- (void)newButtonClick{
    NSLog(@"新增店铺");
    LFShopListViewController *lis = [LFShopListViewController new];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:lis];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
- (void)backItem{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)editClick{
    NSLog(@"编辑");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
